package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.ClassifyGoodsRecycleAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.bean.Goods;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPager;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * 首页进入商品列表
 */
public class GoodsListActivity extends BaseActivity {


    @BindView(R.id.iv_back_activity_goods_list)
    ImageView mIvBackActivityGoodsList;
    @BindView(R.id.iv_cart_activity_goods_list)
    ImageView mIvCartActivityGoodsList;
    @BindView(R.id.rl_search_root_activity_goods_list)
    RelativeLayout mRlSearchRootActivityGoodsList;
    @BindView(R.id.recycle_activity_goods_list)
    YRecycleview mYRecycleview;
    private LoadingPager mLoadingPager;
    private int mClassid;
    private   List<Goods> mGoodsList=new ArrayList<>();
    private ClassifyGoodsRecycleAdapter mClassifyGoodsRecycleAdapter;
    private int currentPage=1;
    private int mType;

    @Override
    protected void initView() {

        mLoadingPager = new LoadingPager(this) {
            @Override
            protected void close() {
                finish();
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(GoodsListActivity.this, R.layout.activity_goods_list);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };
        //setContentView(R.layout.activity_goods_list);
        setContentView(mLoadingPager);
    }
    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        mClassid = intent.getIntExtra(Contans.INTENT_DATA, 0);

        mType = intent.getIntExtra(Contans.INTENT_EXTRA, 1);
        mGoodsList.clear();
        mClassifyGoodsRecycleAdapter=new ClassifyGoodsRecycleAdapter(this,mGoodsList);
        mYRecycleview.setLayoutManager(new LinearLayoutManager(GoodsListActivity.this));
        mYRecycleview.setItemAnimator(new DefaultItemAnimator());
        mYRecycleview.setAdapter(mClassifyGoodsRecycleAdapter);
        mYRecycleview.setRefreshAndLoadMoreListener(new YRecycleview.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                Observable<StatusCode<List<Goods>>> goodListObservable;
                if (mType==1){

              goodListObservable = ApiUtils.getApi().getGoodList(Contans.defaultToken, mClassid,BaseApplication.getUserId(),1,Contans.rows);
                }else {


                    goodListObservable=    ApiUtils.getApi().getchoiceStyleList(mClassid,BaseApplication.getUserId(),Contans.defaultToken,1,Contans.rows);

                }


                HttpUtil.getInstance().toSubscribe(goodListObservable, new ProgressSubscriber<List<Goods>>(GoodsListActivity.this) {
                    @Override
                    protected void _onNext(StatusCode<List<Goods>> listStatusCode) {
                        mYRecycleview.setReFreshComplete();
                        List<Goods> data = listStatusCode.getData();
                        if (listStatusCode.isFlag()&&data!=null &&data.size()>0){
                            mGoodsList.clear();
                            mGoodsList.addAll(data);
                            mClassifyGoodsRecycleAdapter.notifyDataSetChanged();
                            currentPage=1;
                        }else {
                            ToastUtils.makeText("刷新失败");
                        }
                    }
                    @Override
                    protected void _onError(String message) {
                        mYRecycleview.setReFreshComplete();
                        ToastUtils.makeText(message);
                    }
                },"",lifecycleSubject,false,true);
            }
            @Override
            public void onLoadMore() {
                Observable<StatusCode<List<Goods>>> goodListObservable;
                if (mType==1){
                goodListObservable = ApiUtils.getApi().getGoodList(Contans.defaultToken, mClassid,BaseApplication.getUserId(),currentPage+1,Contans.rows);

                }else {
                    goodListObservable=    ApiUtils.getApi().getchoiceStyleList(mClassid,BaseApplication.getUserId(),Contans.defaultToken,currentPage+1,Contans.rows);
                }


                HttpUtil.getInstance().toSubscribe(goodListObservable, new ProgressSubscriber<List<Goods>>(GoodsListActivity.this) {
                    @Override
                    protected void _onNext(StatusCode<List<Goods>> listStatusCode) {
                        mYRecycleview.setloadMoreComplete();
                        List<Goods> data = listStatusCode.getData();
                        if (listStatusCode.isFlag()&&data!=null &&data.size()>0){
                            mGoodsList.addAll(data);
                            mClassifyGoodsRecycleAdapter.notifyDataSetChanged();
                            currentPage++;
                        }else {
                            ToastUtils.makeText("没有更多数据了");
                        }
                    }
                    @Override
                    protected void _onError(String message) {
                        mYRecycleview.setloadMoreComplete();
                        ToastUtils.makeText(message);
                    }
                },"",lifecycleSubject,false,true);
            }
        });
        retry();
    }
    /**
     * 重新加载
     */
    private void retry() {

        Observable<StatusCode<List<Goods>>> goodListObservable;
        if (mType==1){

       goodListObservable = ApiUtils.getApi().getGoodList(Contans.defaultToken, mClassid,BaseApplication.getUserId(),1,Contans.rows);

        }else {
            goodListObservable=    ApiUtils.getApi().getchoiceStyleList(mClassid,BaseApplication.getUserId(),Contans.defaultToken,1,Contans.rows);
        }
        HttpUtil.getInstance().toSubscribe(goodListObservable, new ProgressSubscriber<List<Goods>>(this) {
            @Override
            protected void _onNext(StatusCode<List<Goods>> listStatusCode) {
                List<Goods> data = listStatusCode.getData();
                if (listStatusCode.isFlag()&&data!=null &&data.size()>0){
                     mLoadingPager.showPagerView(Contans.STATE_SUCCEED);
                     currentPage=1;
                     mGoodsList.clear();
                     mGoodsList.addAll(data);
                     mClassifyGoodsRecycleAdapter.notifyDataSetChanged();
                }else {
                    mLoadingPager.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);
                mLoadingPager.showPagerView(Contans.STATE_ERROR);

            }
        },"",lifecycleSubject,false,true);

    }

    @OnClick(R.id.iv_back_activity_goods_list)
    public void onMIvBackActivityGoodsListClicked() {
        finish();
    }

    @OnClick(R.id.iv_cart_activity_goods_list)
    public void onMIvCartActivityGoodsListClicked() {
        if (BaseApplication.isLoginSuccess){
            ShopCarActivity.startActivity(this);

        }else {
            LoginActivity.startActivity(this,true);
        }

    }

    /**
     *
     * @param context
     * @param id
     * @param type   1 商品分类  2 风格精选
     */
    public static void startActivity(Context context,int id,int type){
        Intent intent=new Intent(context,GoodsListActivity.class);
        intent.putExtra(Contans.INTENT_DATA,id);
        intent.putExtra(Contans.INTENT_EXTRA,type);
        context.startActivity(intent);
    }
}
